package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.entity.EntityType;
import com.df.bpm.service.impl.EntityTypeServiceImpl;
import com.df.common.entity.Pagination;
import com.df.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;


/**
 * 主体类型控制器
 */
@Slf4j
@Tag(name = "业务主体控制器")
@RequestMapping("/entityType")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class EntityTypeController {
    /**
     * 主体类型服务
     */
    public final EntityTypeServiceImpl entityTypeServiceImpl;

    /**
     * 获取主体类型信息
     * @param entityTypeIndex 主题类型ID
     */
    @Operation(summary = "获取主体类型信息", description = "获取主体类型信息")
    @GetMapping("id")
    @SaIgnore
    public ResponseEntity<EntityType> getOne(Integer entityTypeIndex) {
        return ResponseEntity.ok(entityTypeServiceImpl.getOne(new EntityType().setEntityTypeIndex(entityTypeIndex)));
    }

    /**
     * 删除主体类型信息
     * @param entityTypeIndex 主题类型ID
     */
    @Operation(summary = "获取主体类型信息", description = "获取主体类型信息")
    @DeleteMapping("")
    @SaIgnore
    public ResponseEntity<Integer> delete(Integer entityTypeIndex) {
        return ResponseEntity.ok(entityTypeServiceImpl.delete(Collections.singleton(entityTypeIndex)));
    }

    /**
     * 更新（主体类型信息）
     * @param entityType 主体类型信息
     */
    @Operation(summary = "更新（主体类型信息）", description = "更新（主体类型信息）")
    @PutMapping("")
    @SaIgnore
    public ResponseEntity<?> update(@RequestBody EntityType entityType){
        if(entityType.getEntityTypeIndex()==null){
            return ResponseEntity.ok(entityTypeServiceImpl.add(entityType));
        }
        return ResponseEntity.ok(entityTypeServiceImpl.update(entityType));
    }

    /**
     * 新增主体类型
     * @param entityType    主体类型
     * @return  新增行数
     */
    @Operation(summary = "新增（主体类型信息）", description = "新增（主体类型信息）")
    @PostMapping("")
    @SaIgnore
    public ResponseEntity<?> newEntityType(@RequestBody EntityType entityType){
        return ResponseEntity.ok(entityTypeServiceImpl.add(entityType));
    }

    /**
     * 获取主体类型信息
     * @param temp 业务主体类型线索
     */
    @Operation(summary = "获取主体类型信息", description = "获取主体类型信息")
    @GetMapping("entityType4Like")
    @SaIgnore
    public ResponseEntity<?> getList4Like(Pagination pagination,EntityType temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(entityTypeServiceImpl.getList4Like(temp)));
        } else {

            return ResponseEntity.ok(entityTypeServiceImpl.getList4Like(temp));
        }
    }

}
